package cn.yr.controller.manage;

import cn.yr.common.util.AjaxResult;
import cn.yr.domain.entity.SysUser;
import cn.yr.service.manage.ISysUserService;
import cn.yr.service.manage.system.TokenService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 个人信息 业务处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/user/profile")
public class SysProfileController extends BaseController {
	@Resource
	private ISysUserService userService;

	@Resource
	private TokenService tokenService;

	/**
	 * 个人信息
	 */
	@GetMapping
	public AjaxResult profile() {
		SysUser user = tokenService.getUser();
		AjaxResult ajax = AjaxResult.success(user);
		return ajax;
	}

	/**
	 * 修改用户
	 */
	@PutMapping
	public AjaxResult updateProfile(@RequestBody SysUser user) {
		if (userService.updateUserProfile(user) > 0) {
			// 更新缓存用户信息
			//TODO
			return AjaxResult.success();
		}
		return AjaxResult.error("修改个人信息异常，请联系管理员");
	}

	/**
	 * 重置密码
	 */
	@PutMapping("/updatePwd")
	public AjaxResult updatePwd(String oldPassword, String newPassword) {

		return AjaxResult.error("修改密码异常，请联系管理员");
	}

	/**
	 * 头像上传
	 */
	@PostMapping("/avatar")
	public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws IOException {
		if (!file.isEmpty()) {

		}
		return AjaxResult.error("上传图片异常，请联系管理员");
	}
}
